﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ServerPlatform.Configuration
{
    public sealed class DataProviderSection : ConfigurationSection
    {
        private static readonly ConfigurationProperty propMapDataProvider;
        private static ConfigurationPropertyCollection properties;
        private static readonly ConfigurationProperty propProviders;


        static DataProviderSection()
        {
            propProviders = new ConfigurationProperty("providers", typeof(ProviderSettingsCollection), null, ConfigurationPropertyOptions.None);
            propMapDataProvider = new ConfigurationProperty("mapDataProvider", typeof(string), "");
            properties = new ConfigurationPropertyCollection();
            properties.Add(propProviders);
            properties.Add(propMapDataProvider);
        }

        public DataProviderSection()
        {
        }

        [StringValidator(MinLength = 1), ConfigurationProperty("mapDataProvider", DefaultValue = "")]
        public string MapDataProvider
        {
            get
            {
                return (string)base[propMapDataProvider];
            }
            set
            {
                base[propMapDataProvider] = value;
            }
        }

        public static IMapDataProvider GetMapDataProviderObject()
        {
            DataProviderSection section = ConfigurationManager.GetSection("DataProviderFramework") as DataProviderSection;
            IMapDataProvider provider = section.GetMapDataProvider();
            return provider;
        }

        public IMapDataProvider GetMapDataProvider()
        {
            IMapDataProvider provider = Activator.CreateInstance(Type.GetType(MapDataProvider)) as IMapDataProvider;
            return provider;
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return properties;
            }
        }

        [ConfigurationProperty("providers")]
        public ProviderSettingsCollection Providers
        {
            get
            {
                return (ProviderSettingsCollection)base[propProviders];
            }
        }
    }
}
